package com.toplabs.unashu.controller;

import com.toplabs.unashu.annotation.ControllerEndpoint;
import com.toplabs.unashu.common.response.CommonResponse;
import com.toplabs.unashu.dto.PageDto;
import com.toplabs.unashu.dto.SearchLog;
import com.toplabs.unashu.entity.Log;
import com.toplabs.unashu.service.LogService;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ityanger
 * @since 2020-08-23
 */
@CrossOrigin
@RestController
public class LogController {

    private final LogService logService;

    public LogController(LogService logService) {
        this.logService = logService;
    }

    /**
     * 日志列表
     *
     * @param searchLog SearchLog
     * @return Result
     */
    @GetMapping("/logs")
    public CommonResponse<PageDto<Log>> getLogList(SearchLog searchLog) {
        PageDto<Log> logList = logService.getLogList(searchLog);
        return CommonResponse.success(logList);
    }

    /**
     * 删除日志
     *
     * @param id Long
     * @return Result
     */
    @DeleteMapping("/log/{id}")
    @ControllerEndpoint(operation = "根据ID删除日志", exceptionMessage = "删除日志失败")
    public CommonResponse<String> delete(@PathVariable("id") Long id) {
        logService.delete(id);
        return CommonResponse.success("删除日志成功！");
    }

    /**
     * 批量删除日志
     *
     * @param ids String
     * @return Result
     */
    @DeleteMapping("/log/batch/{ids}")
    @ControllerEndpoint(operation = "批量删除日志", exceptionMessage = "批量删除日志失败")
    public CommonResponse<Object> batchDelete(@PathVariable("ids") String ids) {
        List<Long> list = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .collect(Collectors.toList());
        logService.batchDelete(list);
        return CommonResponse.success("批量删除日志成功！");
    }
}